<template>
  <div class="app-container">
    <el-card shadow="never">
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="项目名称">
          <el-input v-model="searchForm.proName" clearable> </el-input>
        </el-form-item>
        <el-form-item label="项目分类">
          <el-select v-model="searchForm.proFirstClassUid" placeholder="请选择" @change="changeFirstClass">
            <el-option v-for="item in firstClassNameList" :key="item.uid" :label="item.proFirstClassName"
              :value="item.uid">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleFilter" class="search-button">查询</el-button>
          <el-button type="primary" @click="showAdd" class="search-button">添加</el-button>
          <el-button @click="handleReset" class="search-button">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" fit highlight-current-row>
        <el-table-column align="center" type="index" label="序号" width="55">
        </el-table-column>
        <el-table-column label="项目分类" align="center" prop="proFirstClass">
        </el-table-column>
        <el-table-column label="图片" align="center" prop="fileName">
          <template slot-scope="{ row }">
            <el-image fit="contain" style="width: 100px; height: 100px" :src="row.picImg"
              :preview-src-list="[row.picImg]">
            </el-image>
          </template>
        </el-table-column>
        <el-table-column label="项目名称" align="center" prop="proName">
        </el-table-column>
        <el-table-column label="参考价格（元）" align="center" prop="referencePrice">
        </el-table-column>

        <el-table-column align="center" label="操作" width="200">
          <template slot-scope="{ row }">
            <el-button @click="showDetails(row)" type="text">详情</el-button>
            <el-button @click="showEdit(row)" type="text">编辑</el-button>
            <el-button @click="delHandle(row)" type="text">删除</el-button>

          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" :total="total" :page.sync="searchForm.pageIndex" :limit.sync="searchForm.pageSize"
        @pagination="getList" />
    </el-card>
    <el-dialog :title="dialogTit" :visible.sync="dialogVisible" width="70%">
      <el-form label-position="right" label-width="120px" ref="form" :model="form">
        <el-form-item label="项目分类">
          <el-select v-model="form.proFirstClassUid" placeholder="请选择" @change="changeFirstClass">
            <el-option v-for="item in firstClassNameList" :key="item.uid" :label="item.proFirstClassName"
              :value="item.uid">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="项目名称">
          <el-input v-model.trim="form.proName"></el-input>
        </el-form-item>
        <el-form-item label="图片">
          <uploadImg v-if="dialogVisible" v-model.trim="form.picImg"></uploadImg>
        </el-form-item>
        <el-form-item label="简述">
          <el-input v-model.trim="form.sketch"></el-input>
        </el-form-item>
        <el-form-item label="服务内容">
          <vueUeditor v-if="tinymceStatus.serviceContent" :key="'serviceContent'" v-model="form.serviceContent">
          </vueUeditor>
        </el-form-item>
        <el-form-item label="适用人群">
          <el-input v-model.trim="form.userGroup"></el-input>
        </el-form-item>
        <el-form-item label="服务说明">
          <vueUeditor v-if="tinymceStatus.proDes" :key="'proDes'" v-model="form.proDes"></vueUeditor>
        </el-form-item>
        <el-form-item label="风险告知书">
          <vueUeditor v-if="tinymceStatus.riskNotice" :key="'riskNotice'" v-model="form.riskNotice"></vueUeditor>
        </el-form-item>
        <el-form-item label="参考价格">
          <el-input v-model.trim="form.referencePrice"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" class="btn-cancel">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog title="详情" :visible.sync="detailsDialog" width="70%">
      <el-form label-position="right" label-width="120px" ref="form" :model="form">
        <el-form-item label="项目分类">
          <div>{{ form.proFirstClass }}</div>
        </el-form-item>
        <el-form-item label="项目名称">
          <div>{{ form.proName }}</div>
        </el-form-item>
        <el-form-item label="图片">
          <el-image fit="contain" style="width: 100px; height: 100px" :src="form.picImg"
            :preview-src-list="[form.picImg]">
          </el-image>
        </el-form-item>
        <el-form-item label="简述">
          <div>{{ form.sketch }}</div>
        </el-form-item>
        <el-form-item label="服务内容">
          <div v-html="form.serviceContent"></div>
        </el-form-item>
        <el-form-item label="适用人群">
          <div>{{ form.userGroup }}</div>
        </el-form-item>
        <el-form-item label="服务说明">
          <div v-html="form.proDes"></div>
        </el-form-item>
        <el-form-item label="风险告知书">
          <div v-html="form.riskNotice"></div>
        </el-form-item>
        <el-form-item label="参考价格">
          <div>{{ form.referencePrice }}</div>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="detailsDialog = false" class="btn-cancel">关闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import * as MedicalProject from '@/api/MedicalProject'
  import * as itemOneClass from '@/api/itemOneClass'
  import Pagination from '@/components/Pagination'
  import uploadImg from '../../components/uploadImg.vue'
  import vueUeditor from '../../components/vueUeditor.vue'
  export default {
    components: {
      Pagination,
      uploadImg,
      vueUeditor
    },
    data() {
      return {
        list: null,
        listLoading: true,
        searchForm: {
          pageIndex: 1,
          pageSize: 10
        },
        total: 0,
        dialogVisible: false,
        detailsDialog: false,
        dialogTit: '',
        form: {
          serviceContent: '',
          proDes: '',
          riskNotice: '',
        },
        firstClassNameList: [],
        tinymceStatus: {
          serviceContent: false,
          proDes: false,
          riskNotice: false
        }
      }
    },
    created() {
      this.getList()
      this.getClassName()
    },

    methods: {

      getList() {
        this.listLoading = true
        const param = Object.assign({}, this.searchForm)
        MedicalProject.queryByPage(param).then(response => {
          this.list = response.data.rows
          this.total = response.data.total
          this.listLoading = false
        }).catch(error => {
          this.listLoading = false
          console.log(error)
        })
      },
      handleFilter() {
        this.searchForm.pageIndex = 1
        this.getList()
      },
      handleReset() {
        this.resetTemp();
        this.getList();
      },
      resetTemp() {
        this.searchForm = {
          pageIndex: 1,
          pageSize: 10
        }
      },
      showAdd() {
        this.dialogTit = '新增'
        this.form = {
          serviceContent: '',
          proDes: '',
          riskNotice: '',
        }
        this.dialogVisible = true
        this.tinymceStatus.serviceContent = true
        setTimeout(() => {
          this.tinymceStatus.proDes = true
        }, 500)
        setTimeout(() => {
          this.tinymceStatus.riskNotice = true
        }, 1000)
      },
      showEdit(row) {
        this.dialogTit = '编辑'
        this.form = Object.assign({}, row)
        delete this.form.createTime
        delete this.form.updateTime
        this.dialogVisible = true
        this.tinymceStatus.serviceContent = true
        setTimeout(() => {
          this.tinymceStatus.proDes = true
        }, 500)
        setTimeout(() => {
          this.tinymceStatus.riskNotice = true
        }, 1000)
      },
      showDetails(row) {
        this.form = Object.assign({}, row)
        this.detailsDialog = true
      },
      submit() {
        if (this.dialogTit === '新增') {
          this.creatHandle()
        } else if (this.dialogTit === '编辑') {
          this.editHandle()
        }
      },
      creatHandle() {
        MedicalProject.insert(this.form).then(response => {
          if (response.code == 200) {
            this.dialogVisible = false
            this.getList()
            this.$message.success('新增成功')
          }
        }).catch(error => {
          console.log(error)
        })
      },
      editHandle() {
        MedicalProject.update(this.form).then(response => {
          if (response.code == 200) {
            this.dialogVisible = false
            this.getList()
            this.$message.success('修改成功')
          }
        }).catch(error => {
          console.log(error)
        })
      },
      delHandle(data) {
        this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          const param = {
            uid: data.uid,
          }
          MedicalProject.deleteById(param).then(result => {
            this.$message.success(result.msg)
            this.getList()
          }).catch(error => {
            this.loading = false
            console.log(error)
          })
        }).catch(() => {
          console.log('取消删除')
        });
      },
      //上架
      sj(data) {
        const param = {
          uid: data.uid,
          status: 1
        }
        MedicalProject.update(param).then(response => {
          if (response.code == 200) {
            this.dialogVisible = false
            this.getList()
            this.$message.success('上架成功')
          }
        }).catch(error => {
          console.log(error)
        })
      },
      //下架
      xj(data) {
        const param = {
          uid: data.uid,
          status: 0
        }
        MedicalProject.update(param).then(response => {
          if (response.code == 200) {
            this.dialogVisible = false
            this.getList()
            this.$message.success('下架成功')
          }
        }).catch(error => {
          console.log(error)
        })
      },
      getClassName() {
        itemOneClass.queryByPage({}).then(response => {
          this.firstClassNameList = response.data.rows
        }).catch(error => {
          console.log(error)
        })
      },
      changeFirstClass(e) {
        let proFirstClass = ''
        this.firstClassNameList.some((item) => {
          if (item.uid === e) {
            proFirstClass = item.proFirstClassName
            return true
          }
        });
        if (this.dialogVisible) {
          this.form.proFirstClass = proFirstClass
        }
      }
    }
  }
</script>
